package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.controller.util.Code;
import com.example.controller.util.Data;
import com.example.domain.Community;
import com.example.domain.Life;
import com.example.domain.Volunteer;
import com.example.service.LifeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/life")
public class LifeController {

    @Autowired
    private LifeService lifeService;

    /**
     * 查询所有百姓生活界面！
     */
    @GetMapping
    public com.example.controller.util.Data getAll(){
        List<Life> list= lifeService.list();
        Integer code = list==null ? Code.Code_err :Code.Code_ok;
        String msg = list == null ? "query_err" : "query_ok";
        System.out.println(list);
        return new Data(code,list,msg);
    }

    @GetMapping("/page/{page}/{size}")
    public Data getPage(@PathVariable Integer page, @PathVariable Integer size) {
        IPage<Life> pageUser = lifeService.getPageLife(page, size);
        Integer code = pageUser != null ? Code.Code_ok : Code.Code_err;
        String msg = pageUser != null ? "查询成功" : "查询失败";
        return new Data(code, pageUser, msg);
    }
    @GetMapping("/{id}")
    public Data getById(@PathVariable Integer id){
        Life life = lifeService.getById(id);
        Integer code = life != null ? Code.Code_ok : Code.Code_err;
        String msg = life != null ? "查询成功" : "查询失败";
        return new Data(code,life,msg);
    }

    /**
     * 添加百姓生活动态！
     * @param life
     */
    @PostMapping
    public Data save(@RequestBody Life life){
        boolean save = lifeService.save(life);
        Integer code = save ? Code.Code_ok : Code.Code_err;
        String msg = save ? "添加成功" : "添加失败";
        return new Data(code,msg);
    }

    @PutMapping
    public Data update(@RequestBody Life life){
        boolean b = lifeService.updateById(life);
        Integer code = b ==true? Code.Code_ok : Code.Code_err;
        String msg = b ==true? "修改成功" : "修改失败";
        return new Data(code,msg);

    }

    @DeleteMapping("/{id}")
    public Data deleteById(@PathVariable Integer id){
        boolean b = lifeService.removeById(id);
        Integer code = b ? Code.Code_ok : Code.Code_err;
        String msg = b ? "删除成功" : "删除失败";
        return new Data(code,msg);

    }
}
